<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <!-- tabs标签 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" size="mini" icon="el-icon-plus" @click="addRoleVisible=true">新增角色</el-button>
            <el-table
              border
              :data="roleData"
              style="width: 100%; margin:15px 0;"
            >
              <el-table-column
                label="序号"
                type="index"
                width="80"
              />
              <el-table-column
                label="角色名"
                sortable
                prop="name"
              />
              <el-table-column
                label="描述"
                sortable
                prop="description"
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="{row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="onEdit(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2,5,10,20]"
              :page-size="queryData.pagesize"
              layout="total,sizes,prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">

            <el-row>
              <el-col :span="8" :offset="2">
                <el-form ref="form" label-width="80px">
                  <el-form-item label="企业名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>

          </el-tab-pane>
        </el-tabs>

      </el-card>
    </div>
    <!-- 新增角色弹层 -->
    <el-dialog
      title="新增角色"
      :visible.sync="addRoleVisible"
      width="50%"
      @click="addDiagClose"
    >
      <el-form ref="addRoleForm" :model="addRoleForm" :rules="addRoleRules" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addRoleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleVisible=false">取消</el-button>
        <el-button type="primary" @click="onSave">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompanyInfo } from '@/api/company.js'
import { getRoleList, addRole, editRole } from '@/api/roles'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first', // 高亮
      company: {}, // 公司信息
      roleData: [], // 表格信息
      // 分页
      queryData: {
        page: 1,
        pagesize: 2
      },
      // 显示的总条数
      total: 0,
      // 显示弹层
      addRoleVisible: false,
      // 增角色添加的表单
      addRoleForm: {
        name: '',
        description: ''
      },
      // 新增角色添加的表单验证
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在2-5个长度之间', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在2-20个长度之间', trigger: 'blur' }
        ]

      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.loadCompanyInfo()
    this.getAllRoleList()
  },

  methods: {
    // 加载公司信息
    async  loadCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      // console.log(res)
      this.company = res
    },

    // 获取所有角色列表
    async getAllRoleList() {
      const res = await getRoleList(this.queryData)
      this.roleData = res.rows
      this.total = res.total
      // console.log(this.roleData)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryData.pagesize = val
      this.getAllRoleList()
    },
    // 得到当前页
    handleCurrentChange(val) {
      this.queryData.page = val
      this.getAllRoleList()
    },
    // 点击关闭
    addDiagClose() {
      // el-ui  的对表单进行重置方法
      this.$refs.addRoleForm.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    },
    // 新增角色添加成功
    async onSave() {
      await this.$refs.addRoleForm.validate()
      if (this.addRoleForm.id) {
        // 修改
        await editRole(this.addRoleForm)
        this.$message.success('更新成功')
      } else {
        // 添加
        await addRole(this.addRoleForm)
        this.$message.success('添加成功')
      }
      this.addRoleVisible = false
      this.getAllRoleList()
    },
    // 点击修改
    onEdit(row) {
      this.addRoleVisible = true
      // 根据row.id 发送请求获取最大数据
      this.addRoleForm = { ...row }
    }

  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-tabs__header{
  .el-tabs__item{
    width: 140px;
    font-size: 16px;
    text-align: center;
  }
}
</style>
